package com.yjd.comm.system.util;

import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.cache.up.ICacheUpdate;
import com.yjd.comm.system.model.SysParamModel;
import com.yjd.comm.system.service.ISysParamService;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.ServiceHolder;
import com.yjd.comm.vo.Constant.SysParamKeyEnum;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: DictUtil
 * @Description: 系统参数工具类
 * @author ready likun_557@163.com
 * @date 2014-8-11 下午5:02:24
 */
public class SysParamUtil implements ICacheUpdate {

	private SysParamUtil() {
	}

	private static Logger logger = Logger.getLogger(SysParamUtil.class);
	private static final String SERVICE = "sysParamService";

	/**
	 * 禁止直接访问该list的值,通过getAllDicts()来访问
	 */
	private static List<SysParamModel> sysParamModels = null;

	public static ISysParamService getSysParamService() {
		return ServiceHolder.getLocalService(ISysParamService.class, SERVICE);
	}

	/**
	 * 获取系统参数列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public static List<SysParamModel> getAllDicts() throws Exception {
		if (sysParamModels == null) {
			init();
		}
		return sysParamModels;
	}

	@SuppressWarnings("unchecked")
	private static void init() throws Exception {
		synchronized (SysParamUtil.class) {
			sysParamModels = getSysParamService().getModelList(null,DbWREnums.READ);
		}
	}

	/**
	 * @Title: getDictModel
	 * @Description: 根据系统参数的类型获取系统参数列表
	 * @param sysParamKeyEnum
	 *            系统参数类型
	 * @param @return
	 * @param @throws Exception 设定文件
	 * @return List<SysParamModel> 系统参数列表
	 * @throws
	 */
	public static List<SysParamModel> getSysParamModels(
			SysParamKeyEnum sysParamKeyEnum) throws Exception {
		List<SysParamModel> result = new ArrayList<SysParamModel>();
		List<SysParamModel> list = getAllDicts();
		for (SysParamModel sysParamModel : list) {
			if (sysParamKeyEnum.getValue().equalsIgnoreCase(
					sysParamModel.getParam_key())) {
				result.add(sysParamModel);
			}
		}
		return result;
	}

	/**
	 * 根据系统参数的类型获取系统参数
	 * 
	 * @param sysParamKeyEnum
	 * @return
	 * @throws Exception
	 */
	public static SysParamModel getSysParamModel(SysParamKeyEnum sysParamKeyEnum)
			throws Exception {
		//目前不用缓存所以直接查询
	/*	List<SysParamModel> list = getSysParamModels(sysParamKeyEnum);
		if (list != null && list.size() >= 1) {
			return list.get(0);
		}*/
		if(sysParamKeyEnum==null){
			FrameUtil.throwBaseException1("请求参数不能为空");
		}
	   return	getSysParamService().getModelOne(FrameUtil.newHashMap("param_key",sysParamKeyEnum.getValue()),DbWREnums.READ);
	}

	/**
	 * 根据系统参数类型和值获取系统参数
	 * 
	 * @param sysParamKeyEnum
	 * @param value
	 * @return
	 * @throws Exception
	 */
	public static SysParamModel getSysParamModel(
			SysParamKeyEnum sysParamKeyEnum, String value) throws Exception {
		List<SysParamModel> list = getSysParamModels(sysParamKeyEnum);
		if (list != null) {
			for (SysParamModel sysParamModel : list) {
				if (sysParamModel.getParam_value() == value
						|| (sysParamModel.getParam_value() != null && sysParamModel
								.getParam_value().equals(value))) {
					return sysParamModel;
				}
			}
		}
		return null;
	}
	
	/**
	 * 根据系统参数类型，获取其值
	 * 
	 * @param sysParamKeyEnum
	 * @return
	 * @throws Exception
	 */
	public static String getValue(SysParamKeyEnum sysParamKeyEnum)
			throws Exception {
		SysParamModel sysParamModel = getSysParamModel(sysParamKeyEnum);
		return sysParamModel != null ? sysParamModel.getParam_value() : null;
	}
	
	/**
	* 获取系统参数区间数据
	* @param sysParamKeyEnum 系统参数类型
	* @param targetValue 目标值
	* @return 获取目标值对应的系统参数的值
	* @throws Exception
	 */
	public static Double getParamValueByRegIn(SysParamKeyEnum sysParamKeyEnum,
			Double targetValue) throws Exception {
		List<SysParamModel> list = getSysParamService().getModelList(FrameUtil.newHashMap("param_key"
				,sysParamKeyEnum.getValue()),DbWREnums.READ);
		if (list == null) {
			return null;
		}
		for (SysParamModel sysParamModel : list) {
			if (FrameUtil
					.isRegin(targetValue, sysParamModel.getMin_value(),
							sysParamModel.getMax_value(),
							sysParamModel.getValue_type())) {
				return Double.valueOf(sysParamModel.getParam_value());
			}
		}
		return null;
	}

	public static void refresh() throws Exception{
		init();
	}
	@Override
	public void updateCache() {
		try {
			init();
			logger.info("更新系统参数缓存成功!");
		} catch (Exception e) {
			logger.error("更新系统参数缓存出错:" + e.getMessage(), e);
		}
	}
}
